package com.sxfq.loansupermarket.backsystem.modular.theTest.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.common.persistence.dao.UserMapper;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.User;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.CompletedTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.SearchTestingInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.UserForTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.service.ICompletedTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 已完成考试
 *
 * @author py
 * @Date 2018年2月4日23:05:54
 */
@Controller
@RequestMapping("/completedTest")
public class CompletedTestController extends BaseController {
    @Autowired
    ICompletedTestService completedTestService;
    @Autowired
    UserMapper userMapper;

    @RequestMapping("")
    public String completedTest(Model model) {
        return "/theTest/completedTest.html";
    }

    /**
     * 跳转到考试列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(SearchTestingInfo query) throws Exception {
        Page<CompletedTestInfo> page = new PageFactory<CompletedTestInfo>().defaultPage();
        try {
            List<CompletedTestInfo> completedTestInfoList = completedTestService.selectPageListByQueryGetMap(page, query);
            page.setRecords(completedTestInfoList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    @RequestMapping(value = "/to_details", method = RequestMethod.GET)
    public String enterThePassword(Model model) {
        List<UserForTestInfo> list = completedTestService.selectDetails(ShiroKit.getSessionAttr("theTestId"), ShiroKit.getUser().getId());
        if(list.get(0).getIsAdoption()==1){
            model.addAttribute("isAdoption", "通过考试");
        }else{
            model.addAttribute("isAdoption", "未通过考试");
        }
        model.addAttribute("theTestId", ShiroKit.getSessionAttr("theTestId"));
        model.addAttribute("totalScore", getDailyInterestRate(list.get(0).getTotalScore().toString()));
        model.addAttribute("numberOfQuestion", list.get(0).getNumberOfQuestion());
        model.addAttribute("scoresOfAdoption", list.get(0).getScoresOfAdoption());
        model.addAttribute("testScores", getDailyInterestRate(list.get(0).getTestScores().toString()));
        model.addAttribute("scoresOfQuestion", getDailyInterestRate(list.get(0).getScoresOfQuestion().toString()));
        model.addAttribute("theTestName", list.get(0).getTheTestName());
        model.addAttribute("timeLength", list.get(0).getTimeLength());
        Integer id = ShiroKit.getUser().getId();
        User user = userMapper.selectById(id);
        model.addAttribute("userName", user.getName());
        model.addAttribute("userId", ShiroKit.getUser().getId());
        return "/theTest/completedTesting.html";
    }

    private String getDailyInterestRate(String dailyInterestRate) {
        if (dailyInterestRate.indexOf(".") > 0) {
            //正则表达
            dailyInterestRate = dailyInterestRate.replaceAll("0+?$", "");//去掉后面无用的零
            dailyInterestRate = dailyInterestRate.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点
        }
        return dailyInterestRate;
    }


    /**
     * 跳转到已完成考试详情页面
     */
    @RequestMapping(value = "/to_details", method = RequestMethod.POST)
    public String toTheTestUpdate(Model model) throws Exception {
        String theTestId = super.getPara("theTestId").trim();
        if (ToolUtil.isEmpty(theTestId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        ShiroKit.setSessionAttr("theTestId", theTestId);
        return "/theTest/completedTesting.html";
    }
}
